﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using T.FAS.Runtime.Metadata.Entity.DataModel;
using T.FAS.Runtime.Metadata.Entity;

namespace T.FAS.Runtime.StateMachine.UnitTest
{
    public class DependencyTestTool
    {
        public static string GetMetaJson(string metaTypeID)
        {
            var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "PresetData");
            path = Path.Combine(path, $"{metaTypeID}.json");
            if (!File.Exists(path))
                throw new Exception($"文件:{path}不存在");
            return File.ReadAllText(path);
        }

        public static string GetJointDebuggingJson(string metaTypeID, string dirName = "JointDebugging")
        {
            var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "PresetData", dirName);
            path = Path.Combine(path, $"{metaTypeID}.json");
            if (!File.Exists(path))
                throw new Exception($"文件:{path}不存在");
            return File.ReadAllText(path);
        }

        public static string GetDependentDataJson(string fileName)
        {
            var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "PresetData", "DependentData");
            path = Path.Combine(path, $"{fileName}.json");
            if (!File.Exists(path))
                throw new Exception($"文件:{path}不存在");
            return File.ReadAllText(path);
        }

        public static IMetaDataContent GetMetaData(string metaTypeID)
        {
            var json = GetMetaJson(metaTypeID);
            IMetaDataContent content = default;
            switch (metaTypeID.ToUpper())
            {
                case DataModelConst.DOAbber:
                case DataModelConst.DOMetaType:
                    content = JsonConvert.DeserializeObject<DataObject>(json);
                    break;
                case DataModelConst.SDOAbber:
                case DataModelConst.SDOMetaType:
                    content = JsonConvert.DeserializeObject<DataObject>(json);
                    break;
                case DataModelConst.CDOAbber:
                case DataModelConst.CDOMetaType:
                    content = JsonConvert.DeserializeObject<CompositeDataObject>(json);
                    break;
                case DataModelConst.DQAbber:
                case DataModelConst.DQMetaType:
                    content = JsonConvert.DeserializeObject<DataQuery>(json);
                    break;
                case DataModelConst.EOAbber:
                case DataModelConst.EOMetaType:
                    content = JsonConvert.DeserializeObject<EntityObject>(json);
                    break;
                case DataModelConst.SMAbber:
                case DataModelConst.StateMachineMetaType:
                    content = JsonConvert.DeserializeObject<StateMachineMetadata>(json);
                    break;
                default:
                    break;
            }
            return content;
        }
    }
}
